package com.coocaa.cirrus.common.base.util;

import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

/**
 * @author bijiahao
 * @date : 2025/7/15 14:54
 * @description
 */
@UtilityClass
public class FileUtil {

    public static String removeFileExtension(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return fileName;
        }
        // 处理路径中的文件名
        int lastSeparator = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
        int dotIndex = fileName.lastIndexOf('.');
        // 只有点在最后一个路径分隔符后才认为是扩展名
        if (dotIndex > lastSeparator) {
            return fileName.substring(0, dotIndex);
        }
        return fileName;
    }

    public static String getFileTypeByExtension(String fileName) {
        if (fileName.endsWith(".txt")) {
            return "txt";
        } else if (fileName.endsWith(".pdf")) {
            return "pdf";
        } else if (fileName.endsWith(".md")) {
            return "md";
        } else if (fileName.endsWith(".docx")) {
            return "docx";
        } else if (fileName.endsWith(".xlsx")) {
            return "xlsx";
        } else if (fileName.endsWith(".ppt")) {
            return "ppt";
        } else if (fileName.endsWith(".pptx")) {
            return "pptx";
        } else {
            return null;
        }
    }
}
